package com.yyh.sml;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

public class Demo1 {
    public static void main(String[] args) throws DocumentException {
        // 创建对象
        SAXReader saxReader = new SAXReader();
        // 读取文件
        Document document = saxReader.read("D:\\ideaproject\\IO-code\\XML-code\\src\\com\\yyh\\sml\\Books2\\Books.xml");
        // 获取根元素
        Element rootElement = document.getRootElement();
        System.out.println("根标签的名称："+rootElement.getName());
        //获取根元素下的所有子标签
        List<Element> elements = rootElement.elements();
        for (Element element : elements) {
            System.out.println("\t子标签的名称："+element.getName()+"子标签的id属性值："+element.attributeValue("id"));
            //获取子标签下的所有属性
            Element element1 = element.element("name");
            System.out.println("\t\t属性名称："+element1.getName()+"属性值："+element1.getText());
            Element element2 = element.element("author");
            System.out.println("\t\t属性名称："+element2.getName()+"属性值："+element2.getText());
            Element element3 = element.element("price");
            System.out.println("\t\t属性名称："+element3.getName()+"属性值："+element3.getText());
        }
    }

    public void test1(){

    }
}
